package com.hcrazy.manchester.app.controllers;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;


@Controller
@RequestMapping("/api/validatecode")
public class ValidateCodeController {
	
	public final static int WIDTH = 130;
	public final static int HEIGHT = 53;
	public final static int COUNT = 4;
	public final static int LINE_COUNT = 3;
	
	private char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
	                         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
	                         'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	
	private int x;
	private int y;
	private int fontHeight;
	
	private int width = WIDTH;
	private int height = HEIGHT;
	private int count = COUNT;
	private int lineCount = LINE_COUNT;

	@RequestMapping(method = RequestMethod.GET)
	public void getValidateCode(HttpServletRequest req, HttpServletResponse resp){
		try {
			width = Integer.parseInt(req.getParameter("width"));
		} catch (NumberFormatException e) {
			width = WIDTH;
		}
		try {
			height = Integer.parseInt(req.getParameter("height"));
		} catch (NumberFormatException e) {
			height = HEIGHT;
		}
		try {
			count = Integer.parseInt(req.getParameter("count"));
		} catch (NumberFormatException e) {
			count = COUNT;
		}
		try {
			lineCount = Integer.parseInt(req.getParameter("lineCount"));
		} catch (NumberFormatException e) {
			lineCount = LINE_COUNT;
		}
				
		x = width / (count + 1);
		y = height - 8;;
		fontHeight = height - 6;
		
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		
		Random r = new Random();
		
		g.setColor(new Color(247,254,236));//
		g.fillRect(0, 0, width, height);
		
		Font font = new Font("myfont", Font.ITALIC, fontHeight);
		g.setFont(font);
		
		g.setColor(new Color(241,254,228));
		g.drawRect(0, 0, width-1, height-1);
		
		//
		g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
		for(int i=0;i<lineCount;i++){
			int x = r.nextInt(width/2);
			int y = height/3+r.nextInt(height/5);
			int x1 = 40+r.nextInt(40);
			int y1 = r.nextInt(10);
			g.drawLine(x, y, x+x1,y+y1);
		}
		
		StringBuffer sb = new StringBuffer();
		
		int red = 0;
		int blue = 0;
		int green = 0;
		
		for(int i=0;i<count;i++){
			String str = String.valueOf(chars[r.nextInt(36)]);
			red = r.nextInt(255);
			blue = r.nextInt(255);
			green = r.nextInt(255);
			
			g.setColor(new Color(red,green,blue));
			g.drawString(str, width/9+i*(x-2), y);
			
			sb.append(str);
		}
		
		req.getSession().setAttribute(Constant.SESSION_VALIDATE_CODE, sb.toString().toLowerCase());
		
		resp.setHeader("Pragma", "no-cache"); 
		resp.setHeader("Cache-Control", "no-cache"); 
		resp.setDateHeader("Expires", 0); 

		resp.setContentType("image/jpeg"); 

		
		ServletOutputStream sos;
		try {
			sos = resp.getOutputStream();
			ImageIO.write(image, "jpeg", sos); 
			sos.flush();
			sos.close();
			sos = null;
		} catch (IOException e) {
			Log.error(e.getMessage(), e);
		} 
		
	}
	
}
